#!/usr/bin/env bash
#
# author:yang
# script name: parent_zk_kafka_install.sh
# description: 用于调度安装kafka或者zookeeper脚本,依赖于sub_install_kafka.sh和sub_install_zk.sh脚本
#
INSTALL_USER=root
CURRENT_USER=$(whoami)
REMOTE_USER=root
CURRENT_DIR=`pwd`
SUB_ISNTALL_KAFKA_NAME=sub_install_kafka.sh
SUB_INSTALL_ZK_NAME=sub_install_zk.sh


# 判断执行安装用户是否为root用户,如果执行失败，将错误日志写入安装日志中。

[ "${CURRENT_USER}" != "${INSTALL_USER}" ] && { echo $(date +"%F %H:%M:%S") [ERROR] please run user as root!;exit 1; }



function install_ip() {
    read -p "请输入$1服务部署机器的IP地址,以空格分隔[eg:172.16.0.100 172.16.0.101 172.16.0.102]:" ip_addr
    while :;do
        len=${#ip_addr};
		if [ $len -lt 5 ];then
            read -p "请输入$1服务部署机器的IP地址,以空格分隔[eg:172.16.0.100 172.16.0.101 172.16.0.102]:"  ip_addr
        else
            break;
        fi;
    done;
    ip_addr=`echo ${ip_addr}|tr "[ ,]" " "`
	echo ${ip_addr}
}

function func_ssh() { # 接受传参ip地址
    read -p "是否需要打通ssh免密连接[y/n]:" ssh_conn
    ssh_conn=`echo ${ssh_conn}|tr [a-z] [A-Z]`
    if [ "${ssh_conn}" == "Y" ];then
        read -p "请输入目标主机的root密码:" passwd
        [ -f ~/.ssh/id_rsa.pub ] || ssh-keygen -t rsa -p '' &>/dev/null
        `which expect`|| { echo "脚本依赖expect，检测到未安装，即将开始安装."; yum -y install expect; }
        for i in ${1};do
expect << EOF
        spawn /usr/bin/ssh-copy-id ${REMOTE_USER}@${1}
        expect {
            "yes/no" {send "yes\n";exp_continue}
            "password" {send "$passwd\n"}
        }
        expect eof
EOF

        done;
    fi;
}



function kafka() {
    kafka_ip=$(install_ip 'kafka')
    func_ssh ${kafka_ip}
    read -p "请输入broker.id，以逗号分隔[eg:1,2,3]:" broker_ids
    read -p "请输入zookeeper地址，以逗号分隔[eg:ip1,ip2,ip3]:" zk_addr
    read -p "请输入log.dirs目录,多个目录以逗号分隔[eg: /kafka/data1,/kafka/data2]:" log_dirs
    echo ttttt,,,,zk_addr:${zk_addr}
    zk_addr=`echo ${zk_addr}|tr ',' ' '`
    #echo ttttt,,,,zk_addr:${zk_addr}
    tmp=''
    for i in ${zk_addr};do
        tmp=`echo ${tmp},$i:2181`
       # echo ttttt,,,,tmp:$tmp
    done;
    #echo tttttt,,,,,$tmp
    zk_conn=$(echo `echo ${tmp}|awk -F '^,' '{print $NF}'`/kafka)
    #echo tttttt,,,,,,,$zk_conn
    broker_ids=(`echo ${broker_ids}|tr ',' ' '`)
    index=0
    for ip in ${kafka_ip};do
        broker_id=${broker_ids[$index]}
        echo "scp ${CURRENT_DIR}/${SUB_ISNTALL_KAFKA_NAME} ${REMOTE_USER}@${ip}:~"
        scp ${CURRENT_DIR}/${SUB_ISNTALL_KAFKA_NAME} ${REMOTE_USER}@${ip}:~
        # 将必要的参数传递到远程主机,
	echo "ssh ${REMOTE_USER}@${ip} bash ~/${SUB_ISNTALL_KAFKA_NAME} ${ip} ${broker_id} ${zk_conn} ${log_dirs}"
        ssh ${REMOTE_USER}@${ip} "bash ~/${SUB_ISNTALL_KAFKA_NAME} ${ip} ${broker_id} ${zk_conn} ${log_dirs}"
        index=$(($index+1))
    done;
};

function zk() {
    zk_ip=$(install_ip 'kafka')
    func_ssh ${zk_ip}
    read -p "请输入所有zookeeper 节点 myid,同一集群内每个节点应保持唯一[eg:1,2,3]:" myids
    myids=(`echo $myids|tr ' ,' ' '`)
    idnex=0
    var=''
    for ip in ${zk_ip};do
        myid=${myids[$index]}
        echo "1,,,,$myid"
        var=`echo $var,$ip:$myid`
        echo "2,,,,$var"
        index=$(($index+1))
    done;
    var1=`echo $var|awk -F '^,' '{print $NF}'`
    #echo "zk var 变量 $var1"
    for ip in ${zk_ip};do
        echo $ip
        echo "${CURRENT_DIR}/${SUB_INSTALL_ZK_NAME} ${REMOTE_USER}@${ip}:~" 
        scp ${CURRENT_DIR}/${SUB_INSTALL_ZK_NAME} ${REMOTE_USER}@${ip}:~
        echo "${REMOTE_USER}@${ip} bash ~/${SUB_INSTALL_ZK_NAME} ${var1}"
        ssh ${REMOTE_USER}@${ip} "bash ~/${SUB_INSTALL_ZK_NAME} ${var1}"
    done;

};

echo "此脚本用于调度安装zookeeper和kafka服务,请保证子脚本和父脚本处于同一目录."
sleep 1s

read -p "请输入需要安装的服务[kafka:only install kafka,zk:only install zookeeper,all:install kafka and zk.]:"  service

service=`echo ${service}|tr "[A-Z]" "[a-z]"`
#echo "${service}"

# 判断数据的是否符合标准，不符合退出。
if [ ${service} != 'kafka' -a ${service} != 'zk' -a ${service} != 'all' ];then
    echo "输入有误,请输入需要安装的服务，本次脚本退出"; exit 10;
fi;


if [ ${service} == "kafka" ];then
    kafka;
fi;

if [ ${service} == "zk" ];then
    zk;
fi;

if [ ${service} == "all" ];then
    zk;
    kafka;
fi;



